﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataInterface;
using Model;
using Library.Data.Hibernate;
using Spring.Data.NHibernate.Generic.Support;
using NHibernate;
using NHibernate.Criterion;
using Base.Core.Biz;

namespace Data.Hibernate
{
    public class EmployeeRepository : HibernateDaoSupport, IEmployeeRepository
    {
        #region IEmployeeRepository Members

        public Employee Get(int id)
        {
            return SessionFactory.GetCurrentSession().Get<Employee>(id);
        }

        public IList<Employee> GetAll()
        {
            ICriteria criteria = SessionFactory.GetCurrentSession().CreateCriteria(typeof(Employee));
            return criteria.List<Employee>();
        }

        public long Count()
        {
            return Count(null);
        }

        public long Count(IList<Base.Core.Biz.SearchInfo> searchInfos)
        {
            ICriteria criteria = RepositoryHelper<Employee>.CreateCriteria(this.SessionFactory.GetCurrentSession(), searchInfos);
            criteria.SetProjection(Projections.RowCount());
            object countMayBe_Int32_Or_Int64_DependingOnDatabase = criteria.UniqueResult();

            return Convert.ToInt64(countMayBe_Int32_Or_Int64_DependingOnDatabase);
        }

        public IList<Employee> FindAll(IList<SearchInfo> searchInfos, IList<Base.Core.Biz.OrderInfo> orders)
        {
            return FindAll(searchInfos, null, orders);
        }

        public IList<Employee> FindAll(IList<SearchInfo> searchInfos, PagingInfo pagingInfo, IList<OrderInfo> orders)
        {
            ICriteria criteria = RepositoryHelper<Employee>.CreateCriteria(this.SessionFactory.GetCurrentSession(), searchInfos, orders);

            return criteria.List<Employee>();
        }

        public Employee FindSingle(IList<SearchInfo> searchInfos, IList<OrderInfo> orders)
        {
            IList<Employee> foundList = FindAll(searchInfos, orders);

            if (foundList.Count > 0)
            {
                return foundList[0];
            }

            return null;
        }

        public Employee SaveOrUpdate(Employee entity)
        {
            SessionFactory.GetCurrentSession().SaveOrUpdate(entity);
            return entity;
        }

        public void Delete(Employee entity)
        {
            try
            {
                SessionFactory.GetCurrentSession().Delete(entity);
            }
            catch (Exception ex) { }
        }

        #endregion
    }
}
